﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataModel;
using Mvc4.Areas.Admin.Models;
using Mvc4.Models;
using System.IO;
using BussinesLayer.Services;
using DataAccess.Datatypes;
using System.Web.Helpers;


namespace Mvc4.Areas.Admin.Controllers
{
    public class FriendSitesSiteId
    {
        public int SiteId { get; set; }
        public IEnumerable<FriendSiteSet> FriendSites { get; set; }
    }

    public class SiteAttributesSiteId
    {
        public int SiteId { get; set; }
        public IEnumerable<SiteAttributeSet> SiteAttributes { get; set; }
    }

    public class SiteController : AdmController
    {
        //private ModelContainer db = new ModelContainer();
        private SiteServices ss = new SiteServices();

        //
        // GET: /Admin/Site/

        public ActionResult Index()
        {
            ViewBag.SiteName = "Manage Sites";
            return View(ss.GetSites());
        }

        //
        // GET: /Admin/Site/Create

        public ActionResult Create()
        {
            ViewBag.SiteName = "Create Site";
            UsersInRolesContext uir = new UsersInRolesContext();
            ViewBag.AdminList = uir.GetUsersInRole("Admin");

            ViewBag.FontType = Mvc4.Controllers.StyleController.getFonts();

            return View();
        }

       


        //
        // POST: /Admin/Site/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(SiteSetFile siteset)
        {
            ViewBag.SiteName = "Create Site";
            if (ModelState.IsValid)
            {
                byte[] logo = null;

                if (siteset.File != null)
                {
                    MemoryStream target = new MemoryStream();
                    siteset.File.InputStream.CopyTo(target);
                    logo = target.ToArray();
                }

                
                siteset.Style = ParseStyle(siteset);
                ss.AddSite(siteset.Name, logo, siteset.Style, siteset.GoodType, siteset.AdminId);
                return RedirectToAction("Index");
            }

            return View(siteset);
        }

        //
        // GET: /Admin/Site/Edit/5

        public ActionResult Edit(int id = 0)
        {
            ViewBag.SiteName = "Edit Site";
            Site site = ss.GetSite(id);

            if (site == null)
            {
                return HttpNotFound();
            }

            SiteSetFile siteset = new SiteSetFile();
            siteset.id = site.id;
            siteset.AdminId = site.AdminId;
            siteset.GoodType = site.Description;
            siteset.Name = site.Name;
            siteset.Style = site.Style;
            
            UsersInRolesContext uir = new UsersInRolesContext();
            ViewBag.AdminList = uir.GetUsersInRole("Admin");
            ViewBag.FontType = Mvc4.Controllers.StyleController.getFonts();

            string[] parametros = siteset.Style.Split(',');

            siteset.BtnPrimaryColor = parametros[0];
            siteset.BtnSecondaryColor = parametros[1];
            siteset.BtnOverColor = parametros[2];
            siteset.BtnOverBackg = parametros[3];
            siteset.FontTextColor = parametros[4];
            siteset.FontTextFamily = Int16.Parse( parametros[5]);
            siteset.FontTextOverColor = parametros[6];
            siteset.FontTitleColor = parametros[7];
            siteset.FontTitleFamily = Int16.Parse( parametros[8]);
            siteset.FontTitleOverColor = parametros[9];
            siteset.ImgCalendar = parametros[10];
            siteset.CarouselCaptionColor = parametros[11];


            return View(siteset);
        }

        //
        // POST: /Admin/Site/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(SiteSetFile siteset)
        {
            ViewBag.SiteName = "Edit Site";
            string title = @ViewBag.fontTitleColor;
            if (ModelState.IsValid)
            {
                byte[] logo = null;

                if (siteset.File != null)
                {
                    MemoryStream target = new MemoryStream();
                    siteset.File.InputStream.CopyTo(target);
                    logo = target.ToArray();
                }

                siteset.Style = ParseStyle(siteset);
                ss.ModifySite(siteset.id, siteset.Name, logo, siteset.Style, siteset.GoodType, siteset.AdminId);
                return RedirectToAction("Index");
            }

            return View(siteset);
        }

        private string ParseStyle(SiteSetFile s) 
        {
            string style;
            style = "#" + s.BtnPrimaryColor + ",#";
            style = style + s.BtnSecondaryColor + ",#";
            style = style + s.BtnOverColor + ",#";
            style = style + s.BtnOverBackg + ",#";
            style = style + s.FontTextColor + ",";
            style = style + s.FontTextFamily.ToString() + ",";
            style = style + s.FontTextOverColor + ",#";
            style = style + s.FontTitleColor + ",";
            style = style + s.FontTitleFamily.ToString() + ",#";
            style = style + s.FontTitleOverColor + ",";
            //TODO: ver si se muestra 
            //style = style + s.ImgCalendar + ",";
            style = style + " url(http://localhost:7407/Images/calendarImg/ocean_blue_dayNormal.gif);" + ",#"; ;
            style = style + s.CarouselCaptionColor;

            return style;
        }

        public ActionResult FriendSites(int id = 0)
        {
            ViewBag.SiteName = "News Sites";
            FriendSitesSiteId data = new FriendSitesSiteId();
            data.SiteId = id;
            try
            {
                data.FriendSites = ss.GetSiteData(id).FriendSites.Select(x => new FriendSiteSet
                {
                    id = x.id,
                    Name = x.Name,
                    Url = x.Url,
                    Keyword = x.Keyword,
                    SiteId = x.SiteId
                });

                return View(data);
            }
            catch
            {
                return HttpNotFound();
            }
        }

        public ActionResult SiteAttributes(int id = 0)
        {
            ViewBag.SiteName = "Site Attributes";
            SiteAttributesSiteId data = new SiteAttributesSiteId();
            data.SiteId = id;
            try
            {
                data.SiteAttributes = ss.GetSiteData(id).Attributes.Select(x => new SiteAttributeSet
                {
                    id = x.id,
                    Name = x.Name,
                    SiteId = x.SiteId
                });

                return View(data);
            }
            catch
            {
                return HttpNotFound();
            }
        }

        public ActionResult Error()
        {
            return HttpNotFound();
        }

    }
}